﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLL;
using Model;
using System.Text;
using System.IO;
using System.Data;
using System.Collections;
using System.Reflection;



namespace OA.Controllers
{
    public class UserManagerController : Controller
    {
        //
        // GET: /UserInfo/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult UserInfo()
        {

            return View();
        }

        //加载数据
        public ActionResult LoadData(FormCollection fc)
        {
            int pageSize = Convert.ToInt32(fc["pageSize"]);

            UsersInfoManager UM = new UsersInfoManager();
            List<UsersInfo> Users = UM.GetAll().ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr class=\"DGHeaderStyle\">");

            sb.Append("<th>用户编号</th>");
            sb.Append("<th>用户姓名</th>");
            sb.Append("<th>用户性别</th>");
            sb.Append("<th>手机号码 </th>");
            sb.Append("<th>联系电话</th>");
            sb.Append("<th>Email</th>");
            sb.Append("<th>证件号码</th>");
            sb.Append("<th>生日</th>");
            sb.Append("<th>卡类型</th>");
            sb.Append("<th>卡内余额</th>");
            sb.Append("<th>积分</th>");
            sb.Append(" <th>卡状态</th>");
            sb.Append("<th>登记日期</th>");
            sb.Append("<th>截止日期</th>");
            sb.Append("<th>消费总额</th>");
            sb.Append("<th>备注</th>");
            sb.Append("<th>婚姻</th>");
            sb.Append("<th>邮编</th>");
            sb.Append("<th>住址</th>");
            sb.Append("<th>区域</th>");
            sb.Append("<th>会员类型</th>");
            sb.Append("<th>推介人</th>");
            sb.Append("</tr>");
            for (int i = 0; i < Users.Count; i++)
            {
                if (i < pageSize)
                {
                    sb.Append("<tr>");
                    sb.Append("<td>" + Users[i].memberId + "</td>");
                    sb.Append("<td>" + Users[i].memberName + "</td>");
                    sb.Append("<td>" + Users[i].sex + "</td>");
                    sb.Append("<td>" + Users[i].mobile + "</td>");
                    sb.Append("<td>" + Users[i].Tel + "</td>");
                    sb.Append("<td>" + Users[i].Email + "</td>");
                    sb.Append("<td>" + Users[i].IDNumber + "</td>");
                    sb.Append("<td>" + Users[i].birthDay + "</td>");
                    sb.Append("<td>" + Users[i].cardType + "</td>");
                    sb.Append("<td>" + Users[i].Balance + "</td>");
                    sb.Append("<td>" + Users[i].point + "</td>");
                    sb.Append("<td>" + Users[i].status + "</td>");
                    sb.Append("<td>" + Users[i].createDate + "</td>");
                    sb.Append("<td>" + Users[i].expiresDate + "</td>");
                    sb.Append("<td>" + Users[i].consume + "</td>");
                    sb.Append("<td>" + Users[i].memo + "</td>");
                    sb.Append("<td>" + Users[i].Marriage + "</td>");
                    sb.Append("<td>" + Users[i].Zip + "</td>");
                    sb.Append("<td>" + Users[i].Address + "</td>");
                    sb.Append("<td>" + Users[i].AreasID + "</td>");
                    sb.Append("<td>" + Users[i].MemberType + "</td>");
                    sb.Append("<td>" + Users[i].tuiguangren + "</td>");
                    sb.Append("</tr>");
                }
                else
                {
                    break;
                }
            }
            return Content(sb.ToString());

        }

        //实现分页
        public ActionResult newPage(FormCollection fc)
        {
            int pageNum = Convert.ToInt32(fc["pageNum"]);
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            int maxPage = pageNum * pageSize - 1;
            int minPage = maxPage - pageSize + 1;

            UsersInfoManager UM = new UsersInfoManager();
            List<UsersInfo> Users = UM.GetAll().ToList();
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr class=\"DGHeaderStyle\">");
            sb.Append("<th>用户编号</th>");
            sb.Append("<th>用户姓名</th>");
            sb.Append("<th>用户性别</th>");
            sb.Append("<th>手机号码 </th>");
            sb.Append("<th>联系电话</th>");
            sb.Append("<th>Email</th>");
            sb.Append("<th>证件号码</th>");
            sb.Append("<th>生日</th>");
            sb.Append("<th>卡类型</th>");
            sb.Append("<th>卡内余额</th>");
            sb.Append("<th>积分</th>");
            sb.Append(" <th>卡状态</th>");
            sb.Append("<th>登记日期</th>");
            sb.Append("<th>截止日期</th>");
            sb.Append("<th>消费总额</th>");
            sb.Append("<th>备注</th>");
            sb.Append("<th>婚姻</th>");
            sb.Append("<th>邮编</th>");
            sb.Append("<th>住址</th>");
            sb.Append("<th>区域</th>");
            sb.Append("<th>会员类型</th>");
            sb.Append("<th>推介人</th>");
            sb.Append("</tr>");
            for (int i = minPage; i <= maxPage; i++)
            {
                try
                {
                    sb.Append("<tr oncontextmenu=\"vc('" + Users[i].memberId + "')\" onclick=\"closeContext()\" onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                    sb.Append("<td>" + Users[i].memberId + "</td>");
                    sb.Append("<td>" + Users[i].memberName + "</td>");
                    sb.Append("<td>" + Users[i].sex + "</td>");
                    sb.Append("<td>" + Users[i].mobile + "</td>");
                    sb.Append("<td>" + Users[i].Tel + "</td>");
                    sb.Append("<td>" + Users[i].Email + "</td>");
                    sb.Append("<td>" + Users[i].IDNumber + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(Users[i].birthDay).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + Users[i].cardType + "</td>");
                    sb.Append("<td>" + Convert.ToDecimal(Users[i].Balance).ToString("#0.00") + "</td>");
                    sb.Append("<td>" + Users[i].point + "</td>");
                    sb.Append("<td>" + Users[i].status + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(Users[i].createDate).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + Convert.ToDateTime(Users[i].expiresDate).ToString("yyyy-MM-dd") + "</td>");
                    sb.Append("<td>" + Convert.ToDecimal(Users[i].consume).ToString("#0.00") + "</td>");
                    sb.Append("<td>" + Users[i].memo + "</td>");
                    sb.Append("<td>" + Users[i].Marriage + "</td>");
                    sb.Append("<td>" + Users[i].Zip + "</td>");
                    sb.Append("<td>" + Users[i].Address + "</td>");
                    sb.Append("<td>" + Users[i].AreasID + "</td>");
                    sb.Append("<td>" + Users[i].MemberType + "</td>");
                    sb.Append("<td>" + Users[i].tuiguangren + "</td>");
                    sb.Append("</tr>");
                }
                catch (Exception)
                {

                    break;
                }

            }
            return Content(sb.ToString());
        }

        //最大页数
        public ActionResult Data_Load_maxPage(FormCollection fc)
        {
            int pageSize = Convert.ToInt32(fc["pageSize"]);
            UsersInfoManager UM = new UsersInfoManager();
            List<UsersInfo> Users = UM.GetAll().ToList();
            int count = Users.Count();
            string oop = Math.Ceiling((double)count / pageSize).ToString();
            return Content(oop);
        }

        //用户消费中加载商品列表
        public ActionResult UserSale()
        {
            goodsManager gM = new goodsManager();
            List<goods> gModel = gM.GetAll().ToList();
            // UsersInfoManager UIManager=new  UsersInfoManager();
            //UsersInfo UI=new UsersInfo();
            //string UserId=fc["UserId"];
            //if (!string.IsNullOrEmpty(UserId))
            //{
            //    UI=UIManager.getByUserId(UserId);
            //    ViewData["userId"]=UI.memberId;
            //    ViewData["userName"] = UI.memberName;
            //    ViewData["tel"] = UI.Tel;
            //    ViewData["sex"] = UI.sex;
            //}

            if (gModel.Count > 0)
            {
                return View(gModel);

            }

            return View();
        }
        // 用户消费中根据id获取商品 
        public ActionResult AddGoods(FormCollection fc, string x)
        {


            if (fc["Sindex"] != "" && fc["Sindex"] != string.Empty)
            {
                goodsManager gM = new goodsManager();
                goods gs = new goods();
                int id = Convert.ToInt32(fc["Sval"]);
                gs = gM.GetByID(id);
                return Content(gs.goodsName + "|" + gs.price);

            }
            return View();
        }

        //用户消费中查询用户
        [HttpPost]
        public ActionResult UserSale(FormCollection fc)
        {
            int TestUserId;
            if (int.TryParse(Request.Form["userId"], out TestUserId))
            {
                string userId = Request.Form["userId"];
                UsersInfoManager UM = new UsersInfoManager();
                UsersInfo UI = new UsersInfo();
                UI = UM.getByUserId(userId);
                if (UI != null)
                {
                    ViewData["userId"] = userId;
                    ViewData["userName"] = UI.memberName;
                    ViewData["tel"] = UI.Tel;
                    ViewData["sex"] = UI.sex;
                }
                else
                {
                    return Content("<script>alert('没有这个用户');window.location.href='/UserManager/UserSale'</script>");
                }
            }


            goodsManager gM = new goodsManager();
            List<goods> gModel = gM.GetAll().ToList();
            if (gModel.Count > 0)
            {
                return View(gModel);

            }
            return View();
        }

        //添加用户
        public ActionResult UpdateUser()
        {
            AreasManager AM = new AreasManager();
            UsersInfoManager UM = new UsersInfoManager();
            List<Areas> AllName = AM.getAreaName().ToList();
            List<SelectListItem> cardTypeItems = new List<SelectListItem>();
            List<SelectListItem> sexItems = new List<SelectListItem>();
            List<SelectListItem> marriageItems = new List<SelectListItem>();
            List<SelectListItem> statusItems = new List<SelectListItem>();

            string MaxUserID = UM.getMaxUserId();
            if (MaxUserID == "" || MaxUserID == string.Empty)
            {
                MaxUserID = "0000001";
            }
            else
            {
                MaxUserID = (Convert.ToInt64(MaxUserID) + 1).ToString();
                MaxUserID = MaxUserID.PadLeft(7, '0');
            }

            sexItems.Add(new SelectListItem { Value = "男", Text = "男" });
            sexItems.Add(new SelectListItem { Value = "女", Text = "女" });
            cardTypeItems.Add(new SelectListItem { Value = "金卡", Text = "金卡" });
            cardTypeItems.Add(new SelectListItem { Value = "餐饮", Text = "餐饮" });
            marriageItems.Add(new SelectListItem { Value = "已婚", Text = "已婚" });
            marriageItems.Add(new SelectListItem { Value = "未婚", Text = "未婚" });
            statusItems.Add(new SelectListItem { Value = "正常", Text = "正常" });
            statusItems.Add(new SelectListItem { Value = "挂失", Text = "挂失" });
            statusItems.Add(new SelectListItem { Value = "停用", Text = "停用" });
            var selectList = new SelectList(AllName, "AreaName", "AreaName");
            this.ViewData["areaName"] = selectList;
            this.ViewData["marriage"] = marriageItems;
            this.ViewData["status"] = statusItems;
            this.ViewData["sex"] = sexItems;
            this.ViewData["cardType"] = cardTypeItems;
            this.ViewData["memberId"] = MaxUserID;
            return View();
        }


        [HttpPost]
        public ActionResult UpdateUser(FormCollection fc)
        {
            string memberId = fc["memberId"];
            string memberName = fc["memberName"];
            string index = fc["index"];
            string cardType = fc["cardType"];
            string pwd = fc["pwd"];
            string pwd1 = fc["pwd1"];
            string mobile = fc["mobile"];
            string sex = fc["sex"];
            string email = fc["email"];
            string numberID = fc["numberID"];
            string tel = fc["tel"];
            string birthday = fc["birthday"];
            string status = fc["status"];
            string expiresDate = fc["expiresDate"];
            string createDate = fc["createDate"];
            string marriage = fc["marriage"];
            string areaName = fc["areaName"];
            string zip = fc["zip"];
            string address = fc["address"];
            string memberType = fc["memberType"];
            string tuiguangren = fc["tuiguangren"];
            string memo = fc["memo"];
            if (pwd != pwd1)
            {
                return Content("<script>alert('两次输入的密码不一致');window.location.href='/UserManager/UpdateUser'</script>");
            }
            if (memberName == "" || memberName == string.Empty || mobile == "" || mobile == string.Empty || birthday == "" || birthday == string.Empty || email == "" || email == string.Empty)
            {
                return Content("<script>alert('必填项忘填了');window.location.href='/UserManager/UpdateUser'</script>");
            }
            UsersInfo m = new UsersInfo();
            UsersInfoManager mb = new UsersInfoManager();
            m.memberId = memberId;
            m.memberName = memberName;
            m.memberCardId = memberId;
            m.cardType = cardType;
            if (pwd != "" && pwd != string.Empty)
            {
                m.pwd = pwd;
            }
            else
            {
                m.pwd = "未设置";
            }

            m.mobile = mobile;
            m.sex = sex;
            m.Email = email;
            if (numberID != "" && numberID != string.Empty)
            {
                m.IDNumber = numberID;
            }
            else
            {
                m.IDNumber = "未设置";
            }
            if (tel != "" && tel != string.Empty)
            {
                m.Tel = tel;
            }
            else
            {
                m.Tel = "未设置";
            }
            if (birthday != null && birthday != string.Empty)
            {
                m.birthDay = DateTime.Parse(birthday);
            }
            else
            {
                m.birthDay = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (expiresDate != null && expiresDate != string.Empty)
            {
                m.expiresDate = DateTime.Parse(expiresDate);
            }
            else
            {
                m.expiresDate = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (createDate != null && createDate != string.Empty)
            {
                m.createDate = DateTime.Parse(createDate);
            }
            else
            {
                m.createDate = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            m.status = status;
            if (marriage != "" && marriage != string.Empty)
            {
                m.Marriage = marriage;
            }
            else
            {
                m.Marriage = "未设置";
            }

            m.AreasID = areaName;
            if (zip != "" && zip != string.Empty)
            {
                m.Zip = zip;
            }
            else
            {
                m.Zip = "未设置";
            }
            if (address != "" && address != string.Empty)
            {
                m.Address = address;
            }
            else
            {
                m.Address = "未设置";
            }
            if (memberType != "" && memberType != string.Empty)
            {
                m.MemberType = memberType;
            }
            else
            {
                m.MemberType = "未设置";
            }
            if (tuiguangren != "" && tuiguangren != string.Empty)
            {
                m.tuiguangren = tuiguangren;
            }
            else
            {
                m.tuiguangren = "未设置";
            }
            if (memo != "" && memo != string.Empty)
            {
                m.memo = memo;
            }
            else
            {
                m.memo = "未设置";
            }
            m.Balance = Convert.ToDecimal(0);
            m.point = 0;
            m.consume = Convert.ToDecimal(0);
            m.shifouhuixing = "未设置";
            try
            {
                mb.Add(m);
                return Content("<script>alert('添加成功');window.location.href='/UserManager/UpdateUser'</script>");
            }
            catch
            {
                return Content("<script>alert('添加失败');window.location.href='/UserManager/UpdateUser'</script>");
            }
        }

        //修改用户信息
        public ActionResult editUser(FormCollection fc)
        {

            UsersInfo UI = new UsersInfo();
            UsersInfoManager UIManager = new UsersInfoManager();

            string UserId = System.Web.HttpContext.Current.Request["UserId"];
            UI = UIManager.getByUserId(UserId);

            AreasManager AM = new AreasManager();
            UsersInfoManager UM = new UsersInfoManager();
            List<Areas> AllName = AM.getAreaName().ToList();
            List<SelectListItem> cardTypeItems = new List<SelectListItem>();
            List<SelectListItem> sexItems = new List<SelectListItem>();
            List<SelectListItem> marriageItems = new List<SelectListItem>();
            List<SelectListItem> statusItems = new List<SelectListItem>();
            if (UI.sex == "男")
            {
                sexItems.Add(new SelectListItem { Value = "男", Text = "男", Selected = true });
                sexItems.Add(new SelectListItem { Value = "女", Text = "女" });
            }
            else
            {
                sexItems.Add(new SelectListItem { Value = "男", Text = "男" });
                sexItems.Add(new SelectListItem { Value = "女", Text = "女", Selected = true });
            }
            if (UI.cardType == "金卡")
            {
                cardTypeItems.Add(new SelectListItem { Value = "金卡", Text = "金卡", Selected = true });
                cardTypeItems.Add(new SelectListItem { Value = "餐饮", Text = "餐饮" });
            }
            else
            {
                cardTypeItems.Add(new SelectListItem { Value = "金卡", Text = "金卡" });
                cardTypeItems.Add(new SelectListItem { Value = "餐饮", Text = "餐饮", Selected = true });
            }
            if (UI.Marriage == "已婚")
            {
                marriageItems.Add(new SelectListItem { Value = "已婚", Text = "已婚", Selected = true });
                marriageItems.Add(new SelectListItem { Value = "未婚", Text = "未婚" });
            }
            else
            {
                marriageItems.Add(new SelectListItem { Value = "已婚", Text = "已婚" });
                marriageItems.Add(new SelectListItem { Value = "未婚", Text = "未婚", Selected = true });
            }

            switch (UI.status)
            {
                case "正常":
                    statusItems.Add(new SelectListItem { Value = "正常", Text = "正常", Selected = true });
                    statusItems.Add(new SelectListItem { Value = "挂失", Text = "挂失" });
                    statusItems.Add(new SelectListItem { Value = "停用", Text = "停用" });
                    break;
                case "挂失":
                    statusItems.Add(new SelectListItem { Value = "正常", Text = "正常" });
                    statusItems.Add(new SelectListItem { Value = "挂失", Text = "挂失", Selected = true });
                    statusItems.Add(new SelectListItem { Value = "停用", Text = "停用" });
                    break;
                case "停用":
                    statusItems.Add(new SelectListItem { Value = "正常", Text = "正常" });
                    statusItems.Add(new SelectListItem { Value = "挂失", Text = "挂失" });
                    statusItems.Add(new SelectListItem { Value = "停用", Text = "停用", Selected = true });
                    break;
                default:
                    break;
            }
            statusItems.Add(new SelectListItem { Value = "正常", Text = "正常" });
            statusItems.Add(new SelectListItem { Value = "挂失", Text = "挂失" });
            statusItems.Add(new SelectListItem { Value = "停用", Text = "停用" });
            var selectList = new SelectList(AllName, "AreaName", "AreaName", UI.AreasID);
            this.ViewData["areaName"] = selectList;
            this.ViewData["marriage"] = marriageItems;
            this.ViewData["status"] = statusItems;
            this.ViewData["sex"] = sexItems;
            this.ViewData["cardType"] = cardTypeItems;

            ViewData["memberId"] = UI.memberId;
            ViewData["memberName"] = UI.memberName;
            ViewData["memberId"] = UI.memberId;
            ViewData["pwd"] = UI.pwd;
            ViewData["pwd1"] = UI.pwd;
            ViewData["mobile"] = UI.mobile;
            ViewData["email"] = UI.Email;
            ViewData["tel"] = UI.Tel;
            ViewData["birthday"] = UI.birthDay;
            ViewData["expiresDate"] = UI.expiresDate;
            ViewData["createDate"] = UI.createDate;
            ViewData["zip"] = UI.Zip;
            ViewData["address"] = UI.Address;
            ViewData["memberType"] = UI.MemberType;
            ViewData["tuiguangren"] = UI.tuiguangren;
            ViewData["memo"] = UI.memo;
            return View();
        }

        [HttpPost]
        public ActionResult editUser(FormCollection fc, UsersInfo Users)
        {
            string memberId = fc["memberId"];
            string memberName = fc["memberName"];
            string index = fc["index"];
            string cardType = fc["cardType"];
            string pwd = fc["pwd"];
            string pwd1 = fc["pwd1"];
            string mobile = fc["mobile"];
            string sex = fc["sex"];
            string email = fc["email"];
            string numberID = fc["numberID"];
            string tel = fc["tel"];
            string birthday = fc["birthday"];
            string status = fc["status"];
            string expiresDate = fc["expiresDate"];
            string createDate = fc["createDate"];
            string marriage = fc["marriage"];
            string areaName = fc["areaName"];
            string zip = fc["zip"];
            string address = fc["address"];
            string memberType = fc["memberType"];
            string tuiguangren = fc["tuiguangren"];
            string memo = fc["memo"];
            string x = "window.location.href='/UserManager/editUser/?UserId='" + memberId + "";
            if (pwd != pwd1)
            {
                return Content("<script>alert('两次输入的密码不一致,请重新操作');parent.layer.close(parent.layer.getFrameIndex())</script>");
            }
            if (memberName == "" || memberName == string.Empty || mobile == "" || mobile == string.Empty || birthday == "" || birthday == string.Empty || email == "" || email == string.Empty)
            {
                return Content("<script>alert('必填项忘填了，请重新操作');parent.layer.close(parent.layer.getFrameIndex())</script>");
            }
            UsersInfo m = new UsersInfo();
            UsersInfoManager mb = new UsersInfoManager();
            m = mb.getByUserId(memberId);
            m.memberId = memberId;
            m.memberName = memberName;
            m.memberCardId = memberId;
            m.cardType = cardType;
            if (pwd != "" && pwd != string.Empty)
            {
                m.pwd = pwd;
            }
            else
            {
                m.pwd = "未设置";
            }

            m.mobile = mobile;
            m.sex = sex;
            m.Email = email;
            if (numberID != "" && numberID != string.Empty)
            {
                m.IDNumber = numberID;
            }
            else
            {
                m.IDNumber = "未设置";
            }
            if (tel != "" && tel != string.Empty)
            {
                m.Tel = tel;
            }
            else
            {
                m.Tel = "未设置";
            }
            if (birthday != null && birthday != string.Empty)
            {
                m.birthDay = DateTime.Parse(birthday);
            }
            else
            {
                m.birthDay = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (expiresDate != null && expiresDate != string.Empty)
            {
                m.expiresDate = DateTime.Parse(expiresDate);
            }
            else
            {
                m.expiresDate = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (createDate != null && createDate != string.Empty)
            {
                m.createDate = DateTime.Parse(createDate);
            }
            else
            {
                m.createDate = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            m.status = status;
            if (marriage != "" && marriage != string.Empty)
            {
                m.Marriage = marriage;
            }
            else
            {
                m.Marriage = "未设置";
            }

            m.AreasID = areaName;
            if (zip != "" && zip != string.Empty)
            {
                m.Zip = zip;
            }
            else
            {
                m.Zip = "未设置";
            }
            if (address != "" && address != string.Empty)
            {
                m.Address = address;
            }
            else
            {
                m.Address = "未设置";
            }
            if (memberType != "" && memberType != string.Empty)
            {
                m.MemberType = memberType;
            }
            else
            {
                m.MemberType = "未设置";
            }
            if (tuiguangren != "" && tuiguangren != string.Empty)
            {
                m.tuiguangren = tuiguangren;
            }
            else
            {
                m.tuiguangren = "未设置";
            }
            if (memo != "" && memo != string.Empty)
            {
                m.memo = memo;
            }
            else
            {
                m.memo = "未设置";
            }
            m.Balance = Convert.ToDecimal(0);
            m.point = 0;
            m.consume = Convert.ToDecimal(0);
            m.shifouhuixing = "未设置";
            try
            {
                mb.Update();
                return Content("<script>alert('更新成功');window.parent.location.href='/UserManager/UserInfo'; parent.layer.close(parent.layer.getFrameIndex())</script>");
            }
            catch
            {
                return Content("<script>alert('更新失败,请重新更新');parent.layer.close(parent.layer.getFrameIndex())</script>");
            }
        }

        //删除指定用户
        public ActionResult DelMember(FormCollection fc)
        {
            string UserId = fc["UserId"];
            UsersInfoManager UImanager = new UsersInfoManager();
            if (!string.IsNullOrEmpty(UserId))
            {
                UImanager.DelByUserId(UserId);
                return Content("删除成功");
            }
            return View();
        }

        //查询用户
        public ActionResult UserSelect(FormCollection fc)
        {
            string UserId = fc["UserId"];
            string UserName = fc["UserName"];
            string Sex = fc["Sex"];
            string mobile = fc["mobile"];

            UsersInfo UI = new UsersInfo();
            UsersInfoManager UIManager = new UsersInfoManager();
            List<UsersInfo> Users = new List<UsersInfo>();
            Users = UIManager.SelectByInput(UserId, UserName, Sex, mobile).ToList();
            if (Users.Count() > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("<tr class=\"DGHeaderStyle\">");

                sb.Append("<th>ID </th>  ");
                sb.Append("<th>用户编号</th>");
                sb.Append("<th>用户姓名</th>");
                sb.Append("<th>用户性别</th>");
                sb.Append("<th>手机号码 </th>");
                sb.Append("<th>联系电话</th>");
                sb.Append("<th>Email</th>");
                sb.Append("<th>证件号码</th>");
                sb.Append("<th>生日</th>");
                sb.Append("<th>卡类型</th>");
                sb.Append("<th>卡内余额</th>");
                sb.Append("<th>积分</th>");
                sb.Append(" <th>卡状态</th>");
                sb.Append("<th>登记日期</th>");
                sb.Append("<th>截止日期</th>");
                sb.Append("<th>消费总额</th>");
                sb.Append("<th>备注</th>");
                sb.Append("<th>婚姻</th>");
                sb.Append("<th>邮编</th>");
                sb.Append("<th>住址</th>");
                sb.Append("<th>区域</th>");
                sb.Append("<th>会员类型</th>");
                sb.Append("<th>推介人</th>");
                sb.Append("</tr>");
                for (int i = 0; i < Users.Count(); i++)
                {
                    try
                    {
                        sb.Append("<tr oncontextmenu=\"vc('" + Users[i].memberId + "')\" onmouseover=\"this.style.backgroundColor='#BCD2EE';cursor='default'\" onmouseout=\"this.style.backgroundColor='white'\">");
                        sb.Append("<td>" + Users[i].memberId + "</td>");
                        sb.Append("<td>" + Users[i].memberName + "</td>");
                        sb.Append("<td>" + Users[i].sex + "</td>");
                        sb.Append("<td>" + Users[i].mobile + "</td>");
                        sb.Append("<td>" + Users[i].Tel + "</td>");
                        sb.Append("<td>" + Users[i].Email + "</td>");
                        sb.Append("<td>" + Users[i].IDNumber + "</td>");
                        sb.Append("<td>" + Users[i].birthDay + "</td>");
                        sb.Append("<td>" + Users[i].cardType + "</td>");
                        sb.Append("<td>" + Users[i].Balance + "</td>");
                        sb.Append("<td>" + Users[i].point + "</td>");
                        sb.Append("<td>" + Users[i].status + "</td>");
                        sb.Append("<td>" + Users[i].createDate + "</td>");
                        sb.Append("<td>" + Users[i].expiresDate + "</td>");
                        sb.Append("<td>" + Users[i].consume + "</td>");
                        sb.Append("<td>" + Users[i].memo + "</td>");
                        sb.Append("<td>" + Users[i].Marriage + "</td>");
                        sb.Append("<td>" + Users[i].Zip + "</td>");
                        sb.Append("<td>" + Users[i].Address + "</td>");
                        sb.Append("<td>" + Users[i].AreasID + "</td>");
                        sb.Append("<td>" + Users[i].MemberType + "</td>");
                        sb.Append("<td>" + Users[i].tuiguangren + "</td>");
                        sb.Append("</tr>");
                    }
                    catch (Exception)
                    {

                        break;
                    }

                }
                return Content(sb.ToString());
            }
            else
            {
                return Content("<script>alert('没有找到你要查找的用户')</script>");
            }


        }

        //导出用户
        public ActionResult UserToExcel( FormCollection fc)
        {
            UsersInfoManager UM = new UsersInfoManager();
            List<UsersInfo> Users = UM.GetAll().ToList();
            DataTable dt = new DataTable();
           
            dt=LINQToDataTable<UsersInfo>(Users);
            string fileName = "Users" + DateTime.Now.ToShortDateString().Replace("_","-")+".xls";
            CreateExcel(dt,fileName);
            return View();

        }


        public void CreateExcel(DataTable dt, string fileName)
        {
            HttpResponse resp = System.Web.HttpContext.Current.Response;
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
            string colHeaders = "", ls_item = "";
            ////定义表对象与行对象，同时用DataSet对其值进行初始化
            //DataTable dt = ds.Tables[0];
            DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
            int i = 0;
            int cl = dt.Columns.Count;

            //取得数据表各列标题，各标题之间以t分割，最后一个列标题后加回车符
            for (i = 0; i < cl; i++)
            {
                if (i == (cl - 1))//最后一列，加n
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\n";
                }
                else
                {
                    colHeaders += dt.Columns[i].Caption.ToString() + "\t";
                }
            }
            resp.Write(colHeaders);
            //向HTTP输出流中写入取得的数据信息
            //逐行处理数据 
            foreach (DataRow row in myRow)
            {
                //当前行数据写入HTTP输出流，并且置空ls_item以便下行数据   
                for (i = 0; i < cl; i++)
                {
                    if (i == (cl - 1))//最后一列，加n
                    {
                        ls_item += row[i].ToString() + "\n";
                    }
                    else
                    {
                        ls_item += row[i].ToString() + "\t";
                    }
                }
                resp.Write(ls_item);
                ls_item = "";
            }
            resp.End();

        }
        //将泛型转化成dataTable类型
        public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
        {   //定义要返回的DataTable对象
            DataTable dtReturn = new DataTable();
            // 保存列集合的属性信息数组
            PropertyInfo[] oProps = null;
            if (varlist == null) return dtReturn;//安全性检查
            //循环遍历集合，使用反射获取类型的属性信息
            foreach (T rec in varlist)
            {
                //使用反射获取T类型的属性信息，返回一个PropertyInfo类型的集合
                if (oProps == null)
                {
                    oProps = ((Type)rec.GetType()).GetProperties();
                    //循环PropertyInfo数组
                    foreach (PropertyInfo pi in oProps)
                    {
                        Type colType = pi.PropertyType;//得到属性的类型
                        //如果属性为泛型类型
                        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                        == typeof(Nullable<>)))
                        {   //获取泛型类型的参数
                            colType = colType.GetGenericArguments()[0];
                        }
                        //将类型的属性名称与属性类型作为DataTable的列数据
                        if (pi.Name == "memberId")
                        {
                            dtReturn.Columns.Add(new DataColumn("用户编号", colType));
                        }
                        if (pi.Name == "memberCardId")
                        {
                            dtReturn.Columns.Add(new DataColumn("用户卡号", colType));
                        }
                        if (pi.Name == "memberName")
                        {
                            dtReturn.Columns.Add(new DataColumn("用户姓名", colType));
                        }
                        if (pi.Name == "sex")
                        {
                            dtReturn.Columns.Add(new DataColumn("性别", colType));
                        }
                        if (pi.Name == "mobile")
                        {
                            dtReturn.Columns.Add(new DataColumn("手机", colType));
                        }
                        if (pi.Name == "Tel")
                        {
                            dtReturn.Columns.Add(new DataColumn("电话", colType));
                        }
                        if (pi.Name == "Email")
                        {
                            dtReturn.Columns.Add(new DataColumn("Email", colType));
                        }
                        if (pi.Name == "IDNumber")
                        {
                            dtReturn.Columns.Add(new DataColumn("身份证号", colType));
                        }
                        if (pi.Name == "birthDay")
                        {
                            dtReturn.Columns.Add(new DataColumn("出生日期", colType));
                        }
                        if (pi.Name == "cardType")
                        {
                            dtReturn.Columns.Add(new DataColumn("卡片类型", colType));
                        }
                        if (pi.Name == "Balance")
                        {
                            dtReturn.Columns.Add(new DataColumn("余额", colType));
                        }
                        if (pi.Name == "point")
                        {
                            dtReturn.Columns.Add(new DataColumn("积分", colType));
                        }
                        if (pi.Name == "status")
                        {
                            dtReturn.Columns.Add(new DataColumn("状态", colType));
                        }
                        if (pi.Name == "createDate")
                        {
                            dtReturn.Columns.Add(new DataColumn("办卡日期", colType));
                        }
                        if (pi.Name == "expiresDate")
                        {
                            dtReturn.Columns.Add(new DataColumn("过期日期", colType));
                        }
                        if (pi.Name == "consume")
                        {
                            dtReturn.Columns.Add(new DataColumn("消费", colType));
                        }
                        if (pi.Name == "memo")
                        {
                            dtReturn.Columns.Add(new DataColumn("附加信息", colType));
                        }

                        if (pi.Name == "Marriage")
                        {
                            dtReturn.Columns.Add(new DataColumn("婚姻", colType));
                        }
                        if (pi.Name == "Zip")
                        {
                            dtReturn.Columns.Add(new DataColumn("邮编", colType));

                        }
                        if (pi.Name == "Address")
                        {
                            dtReturn.Columns.Add(new DataColumn("地址", colType));
                        }
                        if (pi.Name == "AreasID")
                        {
                            dtReturn.Columns.Add(new DataColumn("地区", colType));
                        }
                        if (pi.Name == "MemberType")
                        {
                            dtReturn.Columns.Add(new DataColumn("用户类型", colType));
                        }

                        if (pi.Name == "tuiguangren")
                        {
                            dtReturn.Columns.Add(new DataColumn("推广人", colType));
                        }


                    }
                }
                //新建一个用于添加到DataTable中的DataRow对象
                DataRow dr = dtReturn.NewRow();
                //循环遍历属性集合
                foreach (PropertyInfo pi in oProps)
                {   //为DataRow中的指定列赋值

                    if (pi.Name == "memberId")
                    {
                        dr["用户编号"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "memberCardId")
                    {
                        dr["用户卡号"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "memberName")
                    {
                        dr["用户姓名"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "sex")
                    {
                        dr["性别"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "mobile")
                    {
                        dr["手机"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Tel")
                    {
                        dr["电话"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Email")
                    {
                        dr["Email"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "IDNumber")
                    {
                        dr["身份证号"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "birthDay")
                    {
                        dr["出生日期"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "cardType")
                    {
                        dr["卡片类型"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Balance")
                    {
                        dr["余额"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "point")
                    {
                        dr["积分"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "status")
                    {
                        dr["状态"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "createDate")
                    {
                        dr["办卡日期"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "expiresDate")
                    {
                        dr["过期日期"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "consume")
                    {
                        dr["消费"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "memo")
                    {
                        dr["附加信息"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }

                    if (pi.Name == "Marriage")
                    {
                        dr["婚姻"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "Zip")
                    {
                        dr["邮编"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);

                    }
                    if (pi.Name == "Address")
                    {
                        dr["地址"] = pi.GetValue(rec, null) == null ?
                                DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "AreasID")
                    {
                        dr["地区"] = pi.GetValue(rec, null) == null ?
                               DBNull.Value : pi.GetValue(rec, null);
                    }
                    if (pi.Name == "MemberType")
                    {
                        dr["用户类型"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }

                    if (pi.Name == "tuiguangren")
                    {
                        dr["推广人"] = pi.GetValue(rec, null) == null ?
                              DBNull.Value : pi.GetValue(rec, null);
                    }

                }
                //将具有结果值的DataRow添加到DataTable集合中
                dtReturn.Rows.Add(dr);
            }
            return dtReturn;//返回DataTable对象
        }
    }
}
